package com.example.day5_vp;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private List<View> list = new ArrayList<>();
    private MyAdapter myAdapter;
    private ViewPager vp;
    private RadioGroup rg;
    Timer timer;
    Handler handler = new Handler(){};
    int i = 0;//当前滑动的是第几个

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //设置view
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.mipmap.ic_launcher_round);
        ImageView imageView2 = new ImageView(this);
        imageView2.setImageResource(R.mipmap.ic_launcher);
        ImageView imageView3 = new ImageView(this);
        imageView3.setImageResource(R.mipmap.ic_launcher_round);
        //添加view
        list.add(imageView);
        list.add(imageView2);
        list.add(imageView3);
        //实例化适配器
        myAdapter = new MyAdapter(list);
        //绑定适配器
        vp.setAdapter(myAdapter);
        //viewpager滑动时，rg按钮跟着变化
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //滑动完成
            @Override
            public void onPageSelected(int position) {
               switch (position){
                   case 0:
                       rg.check(R.id.rb1);
                       break;
                   case 1:
                       rg.check(R.id.rb2);
                       break;
                   case 2:
                       rg.check(R.id.rb3);
                       break;
               }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });
        //点击rg按钮，viewpager跟着滑动
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        vp.setCurrentItem(2);
                        break;
                }
            }
        });
        //让vp自动滑动起来
        /*timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        vp.setCurrentItem(i);
                        i++;//为了显示下一个
                        if(i==list.size()){
                            //可以重新开始滑动，也可以停止
                            i=0;
                            //停止
                            //timer.cancel();
                        }
                    }
                });
            }
        },0,2000);*/
    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
    }

    public void get(View view) {
        int id = rg.getCheckedRadioButtonId();
        Toast.makeText(MainActivity.this, ""+id, Toast.LENGTH_SHORT).show();
    }
}